package com.dhms.uikit.media

import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter

class ManagerViewPagerAdapter(parent: Fragment, private val mediaList: MutableList<MediaItem>) :
    FragmentStateAdapter(parent) {

    override fun getItemCount(): Int {
        return mediaList.size
    }

    // Set an id different from position when position has been changed.
    override fun getItemId(position: Int): Long {
        return mediaList[position].hashCode().toLong()
    }

    override fun createFragment(position: Int): Fragment {
        val media = mediaList[position]
        return PreviewFragment(media)
    }

    fun removeFragment(index: Int) {
        mediaList.removeAt(index)
        notifyDataSetChanged()
    }
}